package com.api.syh56.shengyunhui.service.impl;

import com.api.syh56.shengyunhui.entity.TmpAddress;
import com.api.syh56.shengyunhui.exception.BaseException;
import com.api.syh56.shengyunhui.mapper.tmpAddressMapper;
import com.api.syh56.shengyunhui.service.TmpAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TmpAddressServiceImpl extends ServiceImpl<tmpAddressMapper, TmpAddress> implements TmpAddressService {

    @Autowired
    tmpAddressMapper tmpAddressMapper;

    @Override
    public void insert(TmpAddress tmpAddress) {
        int insert = tmpAddressMapper.insert(tmpAddress);
        if (insert != 1) {
            throw new BaseException("插入地址失败");
        }
    }

    @Override
    public void deleteById(Long id) {
        if (id == null) {
            throw new BaseException("id为空");
        }
        int deleteById = tmpAddressMapper.deleteById(id);
        if (deleteById != 1) {
            throw new BaseException("删除地址失败");
        }
    }


    @Override
    public void update(TmpAddress tmpAddress) {
        TmpAddress tmpAddressDB = tmpAddressMapper.selectById(tmpAddress.getId());
        if (tmpAddressDB == null) {
            throw new BaseException("id不存在");
        }
        tmpAddress.setId(tmpAddressDB.getId());
        int update = tmpAddressMapper.updateById(tmpAddress);
        if (update != 1) {
            throw new BaseException("修改地址失败");
        }
    }

    @Override
    public List<TmpAddress> selectAll() {
        List<TmpAddress> tmpAddresses = tmpAddressMapper.selectList(null);
        return tmpAddresses;
    }

    @Override
    public TmpAddress selectById(Long id) {
        return tmpAddressMapper.selectById(id);
    }
}
